// SPDX-License-Identifier: MIT
// Copyright 2020 NXP

/*
 * custom.h
 *
 *  Created on: July 29, 2020
 *      Author: nxf53801
 */

#ifndef __CUSTOM_H_
#define __CUSTOM_H_

#ifdef __cplusplus

#include "gui_guider.h"
#include "om.hpp"
#include "driver/gpio.h"
#include "esp_log.h"
#include "bsp/esp-bsp.h"
#include "esp_mac.h"
#include "espnow.h"
#include "espnow_ctrl.h"
#include "espnow_mem.h"
#include "espnow_storage.h"
#include "espnow_utils.h"
#include "firmware.h"
#include "bsp_dart.h"

#include "freertos/semphr.h"

typedef enum
{
    MSG_EMERG_INFO,
    MSG_EMERG_WARN,
    MSG_EMERG_ERROR,
    MSG_EMERG_CRITICAL
} msg_emerg_t;

struct msgBox_msg_t
{
    char title[10];
    char msg[30];
    msg_emerg_t emerg;
};

enum omEvents_m
{
    qPing,
    qOperation,
    pStatus,
    pOperation,
    omReserved,
};

// enum omChannels_m{
//     OM_PUBLIC,
//     OM_PRIVATE,
//     OM_RESERVED,
// };

enum state_m
{
    IDLE,
    IDLE_ACTIVATED,
    ACTIVATED,
    RESERVED
};

enum operation_m
{
    OPERATION_PWR,
    OPERATION_OTA
};

enum pwr_m
{
    PWR_ON,
    PWR_OFF
};

enum ota_m
{
    OTA_TRIGGGER,
    OTA_ENABLE,
    OTA_DISABLE
};

enum errorCode_m
{
    OPERATION_OK,
    OPERATION_FAIL_WIFI,
    OPERATION_FAIL_HARWARE,
    OPERATION_FAIL_BATTERY,
    OPERATION_FAIL_TIMEOUT,
    OPERATION_FAIL_RESERVED
};

#pragma pack(1)

struct stdMsg_t
{
    // char topic;
    uint8_t selfMAC[6];
    uint8_t destMAC[6];
};

struct requestPing_t
{
    stdMsg_t stdMsg;
    uint8_t connectedDevice;
    char deviceMAC[6];
};

struct requestOperation_t
{
    stdMsg_t stdMsg;
    uint16_t session;
    uint16_t operation;
    int operationParam;
};

struct responseStatus_t
{
    stdMsg_t stdMsg;
    uint16_t batteryVoltage;
    uint8_t batteryLevel;
    bool boostState;
    bool chargeState;
    float chargeCurrent;
    float chargeVoltage;
};

struct responseOperation_t
{
    stdMsg_t stdMsg;
    uint16_t session;
    uint16_t errorCode;
};

#pragma pack()

struct dartCOMPack_t
{
    stdMsg_t stdMsg;
    void *data;
    uint8_t size;
    char topic[OM_TOPIC_MAX_NAME_LEN];
};

void _msgbox_callback(msgBox_msg_t &msg, void *arg);
esp_err_t dartEspnowRecvCB(uint8_t *src_addr, void *data, size_t size, wifi_pkt_rx_ctrl_t *rx_ctrl);
void _send_msg(const char *title, const char *msg, msg_emerg_t emerg);
bool _send_msg_topic_cb(msgBox_msg_t &msg, void *arg);

#endif /* EVENT_CB_H_ */

// Function Declaration
void _timer_cb(lv_timer_t *timer);
void _animation_remove_ready_cb(lv_anim_t *a);
void ready_cb(lv_anim_t *a);
void _daemonESPNOW(void *arg);
void btnAllOn_cb(lv_event_t *e);
void btnAllOff_cb(lv_event_t *e);
void swPDEnable_cb(lv_event_t *e);
void printLogWithProgress(const char *label, int progress);
void dartChargerAppSetup(lv_ui *ui);
#endif